#include "..\\Includes\\CMenu.h"
#include "..\\Res\\resource.h"

CMenu::CMenu(HINSTANCE hInstance, HDC hdcSprite)
{
	m_nLastTimeChanged = 0.0f;
	m_currentItemSelected = MenuItems::eMenuItem_Start;
	Rect rect;
	Vec2 center(400, 300);
	Vec2 vel(0.0f, 0.0f);
	m_pBackground = new Sprite(hInstance, IDB_MAINMENUBG, IDB_MAINMENUBGMASK, rect, center, vel);
	m_hdcSprite = hdcSprite;
	m_pStartGameButton = new Sprite(hInstance, IDB_STARTBUTTON, IDB_STARTBUTTONMASK, rect, Vec2(400, 200), vel);
	m_pStartGameButton->update(0.0f); //this will create the bounding rect dynamically.
	m_pSettingsButton = new Sprite(hInstance, IDB_SETTINGSBUTTON, IDB_SETTINGSBUTTONMASK, rect, Vec2(400, 275), vel);
	m_pSettingsButton->update(0.0f);
	m_pHowToButton = new Sprite(hInstance, IDB_HOWTOBUTTON, IDB_HOWTOBUTTONMASK, rect, Vec2(400, 350), vel);
	m_pHowToButton->update(0.0f);
	m_pHighScoresButton = new Sprite(hInstance, IDB_HIGHSCORESBUTTON, IDB_HIGHSCORESBUTTONMASK, rect, Vec2(400, 425), vel);
	m_pHighScoresButton->update(0.0f);
	m_pSelectorRight = new Sprite(hInstance, IDB_SELECTORRIGHT, IDB_SELECTORRIGHTMASK, rect, 
		Vec2(m_pStartGameButton->mBoundingRect.minPt.x, m_pStartGameButton->mBoundingRect.minPt.y + 25), vel);
	m_pSelectorRight->update(0.0f);
		m_pSelectorLeft = new Sprite(hInstance, IDB_SELECTORLEFT, IDB_SELECTORLEFTMASK, rect, 
		Vec2(m_pStartGameButton->mBoundingRect.maxPt.x, m_pStartGameButton->mBoundingRect.maxPt.y - 25), vel);
	m_pSelectorLeft->update(0.0f);

	m_bEnabled = true;
}


CMenu::~CMenu()
{
	if(m_pBackground)
		Deinit();
}


void CMenu::Enable()
{
	m_bEnabled = true;
}

void CMenu::Disable()
{
	m_bEnabled = false;
}

bool CMenu::IsEnabled()
{
	return m_bEnabled;
}

MenuItem CMenu::CurrentItemSelected()
{
	return m_currentItemSelected;
}



void CMenu::NextMenuItem(float time)
{

	if(time - m_nLastTimeChanged < 0.5f)
		return;

	if(m_currentItemSelected < MenuItems::MenuItemLen)
	{
		//next item in the list
		m_currentItemSelected++;
	}
	else
	{
		//loop back to first item in the list
		m_currentItemSelected = MenuItems::eMenuItem_Start;
	}

	UpdateSelector();

	m_nLastTimeChanged = time;

}

void CMenu::PreviousMenuItem(float time)
{

	if(time - m_nLastTimeChanged < 0.5f)
		return;

	if(m_currentItemSelected > 1)
	{
		//previous item in the list
		m_currentItemSelected--;
	}
	else
	{
		//hop to the last item in the list
		m_currentItemSelected = MenuItems::MenuItemLen; //aka eMenuItem_HighScores
	}

	UpdateSelector();

	m_nLastTimeChanged = time;

}

void CMenu::UpdateSelector()
{
	Rect rect(GetMenuItem(m_currentItemSelected)->mBoundingRect);
	rect.minPt.x -= m_pSelectorRight->width() / 2;
	rect.minPt.y += 25;
	rect.maxPt.x += m_pSelectorLeft->width() / 2;
	rect.maxPt.y -= 25;

	m_pSelectorRight->mPosition = rect.minPt;
	m_pSelectorLeft->mPosition = rect.maxPt;

}

void CMenu::Update(HDC hdcFrameBuffer)
{
	Draw(hdcFrameBuffer);
}

void CMenu::Draw(HDC hdcFrameBuffer)
{

	if(m_bEnabled)
	{
		m_pBackground->draw(hdcFrameBuffer, m_hdcSprite);
		m_pStartGameButton->draw(hdcFrameBuffer, m_hdcSprite);
		m_pSettingsButton->draw(hdcFrameBuffer, m_hdcSprite);
		m_pHighScoresButton->draw(hdcFrameBuffer, m_hdcSprite);
		m_pHowToButton->draw(hdcFrameBuffer, m_hdcSprite);
		m_pSelectorRight->draw(hdcFrameBuffer, m_hdcSprite);
		m_pSelectorLeft->draw(hdcFrameBuffer, m_hdcSprite);


#		ifdef DEBUG_DRAW
		LOGPEN logPen;
		HPEN   hPen = NULL, hOldPen = NULL;
		    
		// Set up the rendering pen
		logPen.lopnStyle   = PS_SOLID;
		logPen.lopnWidth.x = 1;
		logPen.lopnWidth.y = 1;

		// Set up the colour, converted to BGR & stripped of alpha
		logPen.lopnColor   = 0x00FFFFFF & RGB2BGR( 0 ); 
		    
		// Create the rendering pen
		hPen = ::CreatePenIndirect( &logPen );
		if (!hPen) return;

		// Select into the frame buffer DC
		hOldPen = (HPEN)::SelectObject( hdcFrameBuffer, hPen );


		
		if(m_currentItemSelected == MenuItems::eMenuItem_Start)
		{
			TextOut( hdcFrameBuffer, 50, 50, _T("Start"), strlen( _T("Start") ) ); 
		}
		else if(m_currentItemSelected == MenuItems::eMenuItem_Settings)
		{
			TextOut( hdcFrameBuffer, 50, 50, _T("Settings"), strlen( _T("Settings") ) ); 
		}
		else if(m_currentItemSelected == MenuItems::eMenuItem_HowTo)
		{
			TextOut( hdcFrameBuffer, 50, 50, _T("HowTo"), strlen( _T("HowTo") ) ); 
		}
		else if(m_currentItemSelected == MenuItems::eMenuItem_HighScores)
		{
			TextOut( hdcFrameBuffer, 50, 50, _T("HighScores"), strlen( _T("HighScores") ) ); 
		}

		
		// Destroy rendering pen
		::SelectObject( hdcFrameBuffer, hOldPen );
		::DeleteObject( hPen );

#		endif //DEBUG_DRAW

	}
}


//Pl0x don't delete the object after you are done with the pointer :)
Sprite * CMenu::GetMenuItem(MenuItem nItem)
{
	switch(nItem)
	{
		case MenuItems::eMenuItem_Start:
			return m_pStartGameButton;
			break;//irrelevant
			
		case MenuItems::eMenuItem_Settings:
			return m_pSettingsButton;
			break;//irrelevant

		case MenuItems::eMenuItem_HowTo:
			return m_pHowToButton;
			break;//irrelevant

		case MenuItems::eMenuItem_HighScores:
			return m_pHighScoresButton;
			break;//irrelevant
			
		default:
			MikeUtils::ASSERT(false, "MenuItem not recognized!!", __FILE__, __LINE__);
			return NULL;
			break; //irrelevant
	}
	MikeUtils::ASSERT(false, "How the hell did I get here?!?!", __FILE__, __LINE__);
	return NULL;


}




void CMenu::Deinit()
{
	MikeUtils::ASSERT(m_pBackground, "Trying to delete background, is it gone already?", __FILE__, __LINE__);
	delete m_pBackground;
	delete m_pStartGameButton;
	delete m_pSettingsButton;
	delete m_pHighScoresButton;
	delete m_pHowToButton;
	delete m_pSelectorRight;
	delete m_pSelectorLeft;
}